// pages/rdt/rdt.js
const feedbackApi = require('../common/showToast.js');
const getToken = require('../../utils/get_token.js');
const formatTime = require('../../utils/util.js');
let openId, md = '', tuanId = '', orderSn = '', time=0;
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  remaind(that);
}
const adzo=function(num) {
	if (num < 10)
		return ('0' + num);
	else
		return num;
}
const fresh=function(data){
	if (data <= 0) {
    clearInterval(that.data.inset);
	return '处理中...';
	} else {
		let d=adzo(parseInt(data/86400));
    let h = adzo(parseInt(data/3600)%24);
    let m = adzo(parseInt((data / 60) % 60));
    let s = adzo(parseInt(data % 60));
    return `还剩${d}天${h}时${m}分${s}秒`;
	}
}
const remaind = (that) => {//获取订单信息
  let params = ['shop/pingTuan/getPinTuanMemberMsgNew', {"memberId":md,"memberOrderSn":orderSn,"tuanId":tuanId}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data=res.data;
    data.applyBackMsg=JSON.parse(data.applyBackMsg);     
    if(data.status=='拒绝退款')
    {
       that.data.btnShow=3;
    }else if(data.status=='申请退款'){            
        if(data.applyBackMsg.state=='60'||data.applyBackMsg.state=='50')
        { that.data.btnShow=1; }
        else if(data.applyBackMsg.state=='61')
        {that.data.btnShow=2; }
    }
    if(data.seeType=='1')//团长显示申请撤销
    {
      that.data.undoShow=true;
    }
     data.item[0].intPrice=(data.item[0].intPrice/100).toFixed(2);
     data.item[0].img=`http://www.globalhexi.com/${data.item[0].img}`;
     data.intRealPrice=(data.intRealPrice/100).toFixed(2);
     data.applyBackMsg.reasonIndex=Number(data.applyBackMsg.reasonIndex)-1;
     data.refundyy=that.data.list[data.applyBackMsg.reasonIndex];
     data.applyBackMsg.retreatEntryDate=formatTime.formatTime(new Date(data.applyBackMsg.retreatEntryDate));
     if(data.applyBackMsg.leaveTimes>0){
         time=data.applyBackMsg.leaveTimes;
         that.data.inset=setInterval(function(){
          that.data.timer=fresh(time);
          time--;
          that.setData({
            timer: that.data.timer,
          })
         },1000);
     }        
     that.data.order=data;
     that.setData({
      btnShow: that.data.btnShow,
      undoShow: that.data.undoShow,
      order: that.data.order,
    })
  }, ...params)
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnShow:0,
    undoShow:false,
    list:['与卖家协商一致','缺货','未按时发货','不想要/错拍','其他','成团无货后台退款'],
    timer:'',
    order:{},
    inset:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    openId = wx.getStorageSync('openId') || '';
    md = wx.getStorageSync('memberId');
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    tuanId = options.tuanId||'';
    orderSn = options.orderSn||'';
    ckToken(that);
  },

  undo:function(){//撤销申请
    let that = this;
    let params = ['shop/order/unApplyforRefund', {"memberId":md,"orderSn":orderSn}]
  getToken.getData(function (res) {
    console.log(res.data)
    if(res.data.flag)
    {
      wx.showToast({
        title: '撤销申请成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(function () {
        wx.redirectTo({
          url:`../p_orderD/p_orderD?id=${orderSn}&tuanId=${tuanId}`,
        })
  
      }, 2000)
    }else{
      feedbackApi.showToast({ title: res.data.message, mask: false })//调用 showToast
    }
  }, ...params)
  },
  agen:function(){
    setTimeout(function () {
      wx.redirectTo({
        url: `../refund/refund?id=${orderSn}&price=${that.data.order.item[0].intPrice}&tuanId=${tuanId}`,
      })
    }, 500)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})